<?php
class Solr extends Controller {

	public $cont;
	public $cont_manage_feeds;
	public $header;
	public $footer;
	public $start;
	public $paginator_config = array();
	public $page = "solr";
	
	public function __construct(){
		parent::Controller();
		
		if(!$this->start){
			$this->start = $this->uri->segment(3);
		}
	   if(!$this->start){
            $this->start = '0';
        }
		
		$this->paginator_config['per_page'] = $this->settings->get_param("per_page");
		$this->paginator_config['num_links'] = $this->settings->get_param("num_links");
		
		
	}
	
	public function index(){
		//Load the header template view
        $this->load->view('common/header_tpl', $this->header);
        
        $this->cont = $this->header;
        
        if($this->uri->segment(4)){
	       $this->session->set_userdata("format", $this->uri->segment(4));
        }
	    $this->cont["format"] = $this->session->userdata("format");
	    
	    
	    if($this->input->post("keywords_search")){
	        $this->session->set_userdata("keywords_search", $this->input->post("keywords_search"));
	    }
	    $this->cont["keywords_search"] = $this->session->userdata("keywords_search");
	    
	    if($this->cont["keywords_search"]){
		    $search_results = $this->solr_manager->query($this->cont["keywords_search"], $this->start, $this->settings->get_param("per_page"));
		    
		    //print_r($search_results);
		    
			$this->cont["total"] = $search_results->response->numFound;
		    $this->cont["docs"] = $search_results->response->docs;
		     
		    
		    $this->load->library('pagination');
			$this->paginator_config['base_url'] = $this->config->item("base_url")."solr/index/";
			$this->paginator_config['total_rows'] = $this->cont["total"];
			$this->pagination->initialize($this->paginator_config);
	
			$this->cont["search_pages"] = $this->pagination->create_links();
	    }
	    
		$this->load->view('main/solr_tpl', $this->cont);
		$this->load->view('common/footer_tpl', $this->footer);
	}

		
	public function manage_feeds(){
		//Load the header template view
        $this->load->view('common/header_tpl', $this->header);
        
		$this->load->database();
	    $this->cont_manage_feeds = $this->header;
	    
	    //if($this->input->post("feed_type_filter")){
	    	$this->session->set_userdata("feed_type_filter", $this->input->post("feed_type_filter"));
	    //}
	    $this->cont_manage_feeds["feed_type_filter"] = $this->session->userdata("feed_type_filter");
	    
	    if($this->input->post("add_feed") == "yes"){
	    	$sel_ex = $this->db->query("SELECT feed_url FROM feeds WHERE feed_url = ?", array($this->input->post("feed_url")));
	    	$res_ex = $sel_ex->row();
	    	if($this->input->post("feed_url") && !$res_ex->feed_url){
	    		
	    	    $this->db->query("
		    	    INSERT INTO feeds 
		    	    (feed_topic, feed_url, feed_type, feed_station, feed_program, last_index) 
		    	    VALUES 
		    	    (?,?,?,?,?,?)", 
		    	    array(
		    	      $this->input->post("feed_topic"),
		    	      $this->input->post("feed_url"),
		    	      $this->input->post("feed_type"),
		    	      $this->input->post("feed_station"),
		    	      $this->input->post("feed_program"), 
		    	      '0')
	    	    );
	    	    
	    	    
	    	}
	    }
	    
	    
	    $sel_sel_feeds = $this->db->query("
	    SELECT DISTINCT feed_type FROM feeds ORDER BY feed_type ASC
        ");
	    $res_sel_feeds = $sel_sel_feeds->result();
	    $this->cont_manage_feeds["res_sel_feeds"] = $res_sel_feeds;
	    
	    
	    
	    $sel_num_feeds = $this->db->query("
        SELECT * FROM feeds WHERE 
        feed_type LIKE '%".$this->cont_manage_feeds["feed_type_filter"]."%' OR 
        feed_program LIKE '%".$this->cont_manage_feeds["feed_type_filter"]."%' OR 
        feed_station LIKE '%".$this->cont_manage_feeds["feed_type_filter"]."%' OR 
        feed_topic LIKE '%".$this->cont_manage_feeds["feed_type_filter"]."%' 
        ");
	    $this->cont_manage_feeds["total"] = $sel_num_feeds->num_rows();
	    
	    $sel_feeds = $this->db->query("
	    SELECT * FROM feeds WHERE 
	    feed_type LIKE '%".$this->cont_manage_feeds["feed_type_filter"]."%' OR 
        feed_program LIKE '%".$this->cont_manage_feeds["feed_type_filter"]."%' OR 
        feed_station LIKE '%".$this->cont_manage_feeds["feed_type_filter"]."%' OR 
        feed_topic LIKE '%".$this->cont_manage_feeds["feed_type_filter"]."%' 
	    ORDER BY ID DESC LIMIT ".$this->start.", ".$this->paginator_config['per_page']."
	    ");
	    $res_feeds = $sel_feeds->result();

	    $this->cont_manage_feeds["res_feeds"] = $res_feeds;
	    
	    $this->load->library('pagination');
        $this->paginator_config['base_url'] = $this->header["BASEURL"] . "solr/manage_feeds/";
        $this->paginator_config['total_rows'] = $this->cont_manage_feeds["total"];
        $this->pagination->initialize($this->paginator_config);
	    
        $this->cont_manage_feeds["feeds_pages"] = $this->pagination->create_links();
	   
	    $this->load->view('main/manage_feeds_tpl', $this->cont_manage_feeds);
        $this->load->view('common/footer_tpl', $this->footer);
	}
	
	public function index_feed(){
	    set_time_limit(0);
	    ini_set("max_execution_time", 0);
	    $feed_id = $this->uri->segment(3);
	    echo "INDEXING STARTED...<br />\n<a href='".$this->header["BASEURL"]."solr/manage_feeds'>GO BACK</a><hr />\n============================================\n<br />";
		if($feed_id != "all_feeds_index"){
		  $this->solr_manager->index_feed_by_id($feed_id, TRUE);
		}else{
		    $sel = $this->db->query("SELECT ID FROM feeds ORDER BY last_index ASC");
            $res = $sel->result();
            foreach($res as $rss){
	            try{
		           @$this->solr_manager->index_feed_by_id($rss->ID, FALSE);
		        }catch (Exception $e){
		        	
		        }
                
            }
            $this->solr_manager->commit_index("all");
		}
	}
	
	public function remove_all_feeds(){
		set_time_limit(0);
		flush();
		echo "REMOVING EVERYTHING...<br />\n<a href='".$this->header["BASEURL"]."solr/manage_feeds'>GO BACK</a><hr />\n============================================\n<br />";
		flush();
		$this->solr_manager->remove_all_feeds();
	}
	
    public function commit_index(){
    	set_time_limit(0);
    	echo "COMITTING INDEX...<br />\n<a href='".$this->header["BASEURL"]."solr/manage_feeds'>GO BACK</a><hr />\n============================================\n<br />";
    	$id = $this->uri->segment(3);
        $this->solr_manager->commit_index($id);
        
    }
    
    public function optimize(){
    	echo "OPTIMIZING INDEX...<br />\n<a href='".$this->header["BASEURL"]."solr/manage_feeds'>GO BACK</a><hr />\n============================================\n<br />";
    	$this->solr_manager->optimize_index();
    }
    
	
    public function remove_feed(){
        set_time_limit(0);
        echo "Removing Feed...<br />\n<a href='".$this->header["BASEURL"]."solr/manage_feeds'>GO BACK</a><hr />\n============================================\n<br />";
        flush();
        $feed_id = $this->uri->segment(3);
        
        $this->solr_manager->remove_feed_by_id($feed_id);
        
    }

	
	
}
?>